<template lang="pug">
  div
    s-table(
      ref="table"
      v-if="this.$route.name === 'deviceManage_register'"
      :column="tableObj.column"
      :filterProps="{items: filterItems}"
      :tableProps="{selection: true}"
      :queryFn="tableObj.getList"
      @selectionChange="rows => selectedRows = rows"
      rowKey="devId"
      title="设备登记"
      )
      .table-operator(slot="table-operator")
        el-button(size="small" type="success" @click="$router.push({ path: '/deviceManage/register/save'})") 添加
        el-button(size="small" type="primary" @click="cameraSync()") 同步
        el-button(size="small" type="warning" @click="moveLoc()" :disabled="selectedRows.length === 0") 移动位置
      template(slot="devType" slot-scope="text, scope") {{dictFilter(text, 'SCEN_TASK_WAY')}}
      template(slot="netStas" slot-scope="text, scope") {{dictFilter(text, 'SCEN_NET_TYPE')}}
      template(slot="action" slot-scope="text, scope")
        el-link(type="primary" :underline="false" @click="$router.push({ path: '/deviceManage/register/save', query: { id: scope.devId, type: 1}})") 详情
        el-link.ml5(type="warning" :underline="false" @click="$router.push({ path: '/deviceManage/register/save', query: { id: scope.devId}})") 编辑
        el-popconfirm(title="确定删除该设备吗？" @confirm="deleteHandle(scope.devId)" @onConfirm="deleteHandle(scope.devId)")
          el-link.ml5(type="danger" :underline="false" slot="reference") 删除
    router-view(v-else)
    org-modal(:visible.sync="moveObj.visible" :id="moveObj.id")
</template>
<script>
import { STable } from '@/components'
import { registerList, cameraInfoSync, deleteHandle } from '@/api/device'
import OrgModal from './OrgModal'

const column = [
  {
    prop: 'devCode',
    label: '设备编号'
  },
  {
    prop: 'devName',
    label: '设备名称'
  },
  {
    prop: 'medinsName',
    label: '所在区域'
  },
  {
    prop: 'prodCode',
    label: '设备序列号',
    align: 'center'
  },
  {
    prop: 'devType',
    label: '设备类型',
    align: 'center'
  },
  {
    prop: 'netStas',
    label: '网络状态',
    align: 'center'
  },
  {
    prop: 'joinCntr',
    label: '接入协议',
    align: 'center'
  },
  {
    prop: 'action',
    label: '操作',
    width: '164',
    align: 'center'
  }
]

export default {
  name: 'DeviceRegisterList',
  components: {
    STable,
    OrgModal
  },
  data () {
    return {
      tableObj: {
        column,
        getList: registerList
      },
      selectedRows: [],
      moveObj: {
        visible: false,
        id: ''
      }
    }
  },
  computed: {
    filterItems () {
      return [
        {
          key: 'keyword',
          label: '关键信息'
        },
        {
          key: 'devType',
          label: '设备类型',
          type: 'selectdict',
          dictKey: 'SCEN_DEV_TYPE'
        }
      ]
    }
  },
  created () {
  },
  methods: {
    // 刷新表格
    refreshTable (flag = false) {
      this.$refs.table.refresh(flag)
    },
    cameraSync () {
      cameraInfoSync().then(() => {
        this.$message.success('同步成功')
      })
    },
    // 移动位置
    moveLoc () {
      const { selectedRows } = this
      this.moveObj = {
        visible: true,
        id: selectedRows.map(x => x.devId).toString()
      }
    },
    deleteHandle (id) {
      deleteHandle(id).then(() => {
        this.$message.success('删除成功')
        this.refreshTable()
      })
    }
  }
}
</script>
